package de.hm.edu.kinderfinder.activities;

import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.TimePicker;
import de.hm.edu.kinderfinder.contentprovider.DataBaseManager;
import de.hm.edu.kinderfinder.pojos.ZoneObject;

/**
 * Diese Activity zeigt die Zeiteinstellungen für eine Zone an. Man kann einer
 * Zone einen Zeitbereich geben und sie auch für beliebig viele Wochentage
 * konfigurieren. Die Activity liest die Daten und zeigt sie an und speichert
 * Änderungen wenn man auf Save klickt.
 * 
 * @author Christoph-Samuel Pitter
 * 
 */
public class ZoneTimeConfigActivity extends Activity {

	private List<ZoneObject> zones;
	private TimePicker timePickerFrom;
	private CheckBox cbMon;
	private CheckBox cbTue;
	private CheckBox cbWed;
	private CheckBox cbThur;
	private CheckBox cbFri;
	private CheckBox cbSat;
	private CheckBox cbSun;
	private TimePicker timePickerTo;
	private ZoneObject selectedZone;

	/**
	 * Beim erstellen werden alle Werde von der entsprechenden Zone aus
	 * der Datenbank gelesen und die Settings auf der Anzeige entsprechend
	 * eingestelt.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.zone_time_config);

		zones = ZoneListActivity.zones;
		Intent intent = getIntent();

		// TODO:very stupid, should have used a map!!!
		String zonename = intent.getExtras()
				.getString(ZoneListActivity.ZONE_ID);
		for (ZoneObject z : zones) {
			if (z.getZonename().equals(zonename)) {
				selectedZone = z;
				continue;
			}
		}

		TextView tfZoneName = (TextView) findViewById(R.id.zoneNameField);
		tfZoneName.setText(selectedZone.getZonename());

		timePickerFrom = (TimePicker) findViewById(R.id.timePicker1);
		timePickerTo = (TimePicker) findViewById(R.id.timePicker2);

		Date startTime = new Date(selectedZone.getTimeStart());
		Date endTime = new Date(selectedZone.getTimeEnd());

		timePickerFrom.setCurrentHour(startTime.getHours());
		timePickerFrom.setCurrentMinute(startTime.getMinutes());
		timePickerTo.setCurrentHour(endTime.getHours());
		timePickerTo.setCurrentMinute(endTime.getMinutes());

		cbMon = (CheckBox) findViewById(R.id.checkBoxMon);
		cbTue = (CheckBox) findViewById(R.id.checkBoxTue);
		cbWed = (CheckBox) findViewById(R.id.checkBoxWed);
		cbThur = (CheckBox) findViewById(R.id.checkBoxThur);
		cbFri = (CheckBox) findViewById(R.id.checkBoxFri);
		cbSat = (CheckBox) findViewById(R.id.checkBoxSat);
		cbSun = (CheckBox) findViewById(R.id.checkBoxSun);

		cbMon.setChecked(selectedZone.isMonday());
		cbTue.setChecked(selectedZone.isTuesday());
		cbWed.setChecked(selectedZone.isWednesday());
		cbThur.setChecked(selectedZone.isThursday());
		cbFri.setChecked(selectedZone.isFriday());
		cbSat.setChecked(selectedZone.isSaturday());
		cbSun.setChecked(selectedZone.isSunday());
	}

	/**
	 * Speichert alle Einstellungen in der Datenbank und schliesst die
	 * View.
	 * @param view
	 */
	public void saveAndClose(View view) {
		// delete old zone from db
		selectedZone.setMonday(cbMon.isChecked());
		selectedZone.setTuesday(cbTue.isChecked());
		selectedZone.setWednesday(cbWed.isChecked());
		selectedZone.setThursday(cbThur.isChecked());
		selectedZone.setFriday(cbFri.isChecked());
		selectedZone.setSaturday(cbSat.isChecked());
		selectedZone.setSunday(cbSun.isChecked());

		Date fromTime = new Date();
		fromTime.setHours(timePickerFrom.getCurrentHour());
		fromTime.setMinutes(timePickerFrom.getCurrentMinute());

		Date toTime = new Date();
		toTime.setHours(timePickerTo.getCurrentHour());
		toTime.setMinutes(timePickerTo.getCurrentMinute());

		selectedZone.setTimeStart(fromTime.getTime());
		selectedZone.setTimeEnd(toTime.getTime());

		DataBaseManager.getInstance(this).addTimeValuesToZone(selectedZone);
		finish();

	}

	public void close(View view) {

		finish();
	}

}
